package com.fingard.dsp.bank.directbank.ceb02;

import com.fingard.dsp.bank.directbank.bankInterface.IQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.ReqQryQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfo;
import com.fingard.dsp.bank.reqrespobj.qryquotainfo.RetAtsQuotaInfoDetail;
import com.fingard.xml.XmlTextReader;
import com.fingard.xml.XmlTextWriter;

import java.math.BigDecimal;

/**
 * 获取额度信息
 * @author F
 *
 */
public class CEB02QryQuotaInfo extends CEB02Base implements IQuotaInfo{

	@Override
	public RetAtsQuotaInfo queryQuotaInfo(ReqQryQuotaInfo reqQryQuotaInfo) throws Exception {
		RetAtsQuotaInfo retAts = new RetAtsQuotaInfo(reqQryQuotaInfo.reqHeader);
		retAts.writeStart();
		String tmpRespCode = "";
        String tmpRespInfo = "";
        try {
            CEB02Header bankHeader = new CEB02Header();
            //银行获取相关信息
            bankHeader.FTranCode = "Q050";
            XmlTextWriter xmlWriter = new XmlTextWriter();//发银行报文的XML对象
            //写入通用的头部节点
            writeCommonHeader_3(xmlWriter, bankHeader);    
            //写入body
            xmlWriter.writeStartElement("Body");
            xmlWriter.writeElementString("CHANNEL_CODE", bankFront.org);//接入机构号
            xmlWriter.writeElementString("DEALER_CODE", reqQryQuotaInfo.DealerCode);//经销商编码 bankFront.corpNo
            xmlWriter.writeElementString("LIMIT_TYPE", "1");//1-综合授信
            xmlWriter.writeElementString("AMOUNT_STATUS", "1");//1-正常
            xmlWriter.writeEndElement();
            xmlWriter.writeEndDocument();
            String[] tmpStrRet = sendToBank(xmlWriter.toXmlString(),bankHeader);//发送银行
            if(tmpStrRet[0].length() == 0){
                StringBuilder tmpSbMsg = new StringBuilder();                        
                //解析银行返回
                XmlTextReader tmpBkReader = loadBankRespXmlReader(tmpStrRet[1], tmpSbMsg);
                if(tmpBkReader != null){
                    	String tmpRetCode = tmpBkReader.getFirstText("/Out/Head/ErrCode");
                    	String tmpRetMsg = tmpBkReader.getFirstText("/Out/Body/ErrorInfo");
                    	if (tmpRetCode.equals("")) {
                    		tmpRespCode = "0";
                            tmpRespInfo = "获取成功";
                            while (tmpBkReader.read("/Out/Body")) {
                            	RetAtsQuotaInfoDetail tmpRetDetail = new RetAtsQuotaInfoDetail();
                                tmpRetDetail.DealerCode = reqQryQuotaInfo.DealerCode;
                                tmpRetDetail.LimitBeginDate = tmpBkReader.readedXElement.getSingleText("AMOUNT_STAR_DATE");
                                tmpRetDetail.LimitEndDate = tmpBkReader.readedXElement.getSingleText("AMOUNT_END_DATE");
                                tmpRetDetail.Limit = tmpBkReader.readedXElement.getSingleText("TOTAL_CREDIT_EXPOSURE");
                                tmpRetDetail.UsedLimit = tmpBkReader.readedXElement.getSingleText("TOTAL_EXPOSURE");
                                if(tmpRetDetail.Limit!=null && tmpRetDetail.UsedLimit!=null){
                                    tmpRetDetail.UnuseLimit = new BigDecimal(tmpRetDetail.Limit).subtract(new BigDecimal(tmpRetDetail.UsedLimit)).toString();
                                }else{
                                    tmpRetDetail.UnuseLimit = tmpRetDetail.Limit;
                                }
                                tmpRetDetail.Reserve1 = "";
                                tmpRetDetail.Reserve2 = "";
                                tmpRetDetail.Reserve3 = "";
                                tmpRetDetail.Reserve4 = "";
                                tmpRetDetail.Reserve5 = "";
                                retAts.writeRetDetail(tmpRetDetail);
                            }
                        } else {
                        	tmpRespCode = "-1";
                            tmpRespInfo = tmpRetMsg;
                        }
                } else {
                    tmpRespCode = "-1";
                    tmpRespInfo = tmpSbMsg.toString();
                }
            } else {
                tmpRespCode = "-1";
                tmpRespInfo = tmpStrRet[1];
            }
        } catch (Exception e) {
            tmpRespCode = "-1";
            tmpRespInfo = e.getMessage();
            e.printStackTrace();
        }
        retAts.writeRespCode(tmpRespCode);
        retAts.writeRespInfo(tmpRespInfo);
        retAts.writeEnd();
        return retAts;
    }

}
